--[[/**
 * [0] 降低所受伤害
 * [1] 剩余伤害的 N 分摊给所有队友(包括召唤物)
 * [2] 低于N 的队友不算
-- *
-- */]]

---@class BuffEffect484102 : BaseBuffEffect
BuffEffect484102 = ClientFight.CreateClass("BuffEffect484102", ClientFight.BaseBuffEffect)
local t = BuffEffect484102

---@param fight Fight
---@param buff FighterBuff
function t:action(fight, buff, ...)
    local parameters = {...};
    local source = buff.source;
    local target = buff.target;
    if (table.getn(parameters) < 3) then
        return IBuffEffect.FAIL;
    end

    local result = parameters[3];
    if (not result:isDamage()) then
        return IBuffEffect.FAIL;
    end
    if (not AttackUtils.isDamage(result.state)) then
        return IBuffEffect.FAIL;
    end
    local ratio = buff.buffBean.f_BuffEffectValue[1] * buff.overlay;
    local oldDamage = result.damage;
    local jianmianDamage = calcRatio(result.damage, ratio);
    result.damage = result.damage - jianmianDamage;
    ratio = buff.buffBean.f_BuffEffectValue[2] * buff.overlay;
    local jianmianDamage2 = calcRatio(result.damage, ratio);
    result.damage = result.damage - jianmianDamage2;

    local oursBy = FighterUtils.getOursBy(target, true, nil, true);
    local results = {};
    for k, fighter in pairs(oursBy) do
        if (fighter == target) then
            goto continue;
        end
        if (fighter:isSummons() and not fighter.isRealSummons()) then
            goto continue;
        end
        if (FighterStateEnum.is(fighter, FighterStateEnum.CAN_NOT_DAMAGE_BY_ENEMY)) then
            goto continue;
        end
        if (FighterStateEnum.is(fighter, FighterStateEnum.IMMUNITY)) then
            goto continue;
        end
        if (FighterStateEnum.is(fighter, FighterStateEnum.IMMUNITY_PHYSICAL_DAMAGE)) then
            goto continue;
        end
        if (FighterStateEnum.is(fighter, FighterStateEnum.IMMUNITY_MAGIC_DAMAGE)) then
            goto continue;
        end
        local hp = fighter.hp;
        local v = hp / FighterAttributeManager.getFightAttr(fighter, FighterAttributeEnum.HP) * 10000;
        if (v < buff.buffBean.f_BuffEffectValue[3]) then
            goto continue;
        end
        if ( table.contains(fighter.totalSign.signs,FighterSignEnum.TAG_1006) ) then
            goto continue;
        end
        table.insert(results, fighter);
        :: continue ::
    end
    local attacker = parameters[1];
    local reduceHp = (jianmianDamage2 / table.getn(results)); --每个人分摊的伤害
    local recordDamage = 0;
    for k, fighter in pairs(results) do
        recordDamage = recordDamage + DamageManager.setFighterHp(fight, fighter, attacker, nil, DamageResultTypeEnum.NORMAL, -reduceHp,
                fighter.hp - reduceHp, true, true, true);
    end

    SkillRoleMgr.savePathFile(s"[" .. fight.frame .. "][EFFECT]Fighter " .. buff.target.fighterId .. " Buff:"
            .. buff.modelId .. " 触发, 减免前总伤害 : " .. oldDamage .. " ,减免伤害: " .. jianmianDamage .. " ,分摊总伤害: " .. jianmianDamage2
            .. " ,分摊人数: " .. results .. " ,每人分摊伤害: " .. reduceHp);
    return ClientFight.BaseBuffEffect.action(self, fight, buff, ...);
end

t.New()